package Components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import smash_tris.Room;

import Connector.Connector;
import Equipment.*;

public class GameOptionPanel extends JPanel implements Runnable{
	
	private GameWindow motherDialog;
	private Connector connector;
	private ImageButton exit;
	private ImageButton starter;
	private Rocket rocket;
	private Helper helper;
	private JLabel user1, user2;
	private JLabel user1Score, user2Score;
	private GameOptionPanel GOP;
	private Thread waitingThread = null;
	private Integer starterResponse = 0;
	
	private String DIR = "picture/menu_pics/";
	private BufferedImage background;
	
	private boolean running;
	private boolean ready = false;
	private Room room = null;
	private boolean playerNames = true;
	
	public GameOptionPanel(GameWindow mother) {
		
		this.motherDialog = mother;
		this.room = this.motherDialog.getRoom();
		
		try {
			background = ImageIO.read(new File(DIR + "GOP.png"));
		} catch (IOException e) {
			System.err.println("B��d podczas �adowania t�a OptionPanel");
			e.printStackTrace();
		}
		this.connector = motherDialog.getConnection();
		this.setLayout(null);
		this.setVisible(true);
		this.setSize(204, motherDialog.getY());
		
		this.repaint();
		initComponents();
		
		running = false;
		//this.setStart();
	}

	private void initComponents() {

    	exit = new ImageButton(DIR+"exit_UP.png",DIR+"exit_DOWN.png");
    	starter = new ImageButton(DIR+"ready_DOWN.png",DIR+"ready.png");
    	starter.setDisable(DIR+"ready_DOWN.png");
    	rocket = new Rocket();
    	helper = new Helper();
    	JLabel tekst = new JLabel("Ready??");
    	user1 = new JLabel();
    	user2 = new JLabel();    	
    	
    	/*user1 = new JLabel(motherDialog.getRoom().getPlayers().get(0).getNick());
    	if(motherDialog.getRoom().getPlayers().size() > 1)
    	user2 = new JLabel("enemy");
    	else
    	user2 = new JLabel();*/
    	
    	user1Score = new JLabel("0");
    	if(motherDialog.getRoom().getPlayers().size() > 1)
    	user2Score = new JLabel("0");  	
    	else 
    	user2Score = new JLabel("0"); 	
    	
    	exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                motherDialog.setVisible(false);
                //motherDialog.closeWindow();
                synchronized (motherDialog) {
                	motherDialog.setFirst();
                	motherDialog.dispose();
                    
				}
                GameOptionPanel.this.setStop();
                
               
            	motherDialog.requestFocusInWindow();
            	motherDialog = null;
            	
            	//System.exit(-1);
            }
        });
    	
    	
    	
    	starter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                
                playerNames = false;
                starterResponse = connector.setReady(room);
                if (starterResponse == 0) {
                	playerNames = true;
                	ready = false;
                	starter.setChange();
                	System.out.println("response == 0");
                } else if (starterResponse == 1) {
                	
                	ready = true;
                	starter.setChange();
                	System.out.println("response == 1");
                	
                	/*waitingThread = new Thread(new Runnable() {
						public void run() {
							while (starterResponse == 1) {
								if (connector.isGameStarted()) {
									System.out.println("waiting thread...");
									motherDialog.getTetrisPlayground().
										getMyTetris().start();
									starterResponse = 2;
								}
							}
						}
					});*/
                	
                	//waitingThread.start();
                	
                } else if (starterResponse == 2) {
                	System.out.println("response == 2");
                	ready = true;
                	//motherDialog.getTetrisPlayground().getMyTetris().start();
                	starter.setEnabled(false);
                }
                
                motherDialog.requestFocusInWindow();
            }
        });
    	
    	rocket.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	motherDialog.requestFocusInWindow();
            	rocket.method(motherDialog);
            }
        });
    	
    	helper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	motherDialog.requestFocusInWindow();
            	helper.method(motherDialog);
            }
        });  	
    	
    	rocket.setBounds(50, 200, 69, 31);
    	helper.setBounds(50, 250, 69, 31);
    	user1Score.setBounds(100, 125, 80, 10);
    	user1.setBounds(10, 125, 90, 20);
    	user2Score.setBounds(100, 145, 80, 10);
    	user2.setBounds(10, 145, 90, 20);
    	exit.setBounds(69, 350, 62, 62);
    	starter.setBounds(100, 25, 60,60);
    	tekst.setBounds(10, 50, 70, 15);
    	
    	
    	this.add(exit);
    	this.add(user1Score);
    	this.add(user2Score);
    	this.add(user1);
    	this.add(user2);
    	this.add(rocket);
    	this.add(helper);
    	this.add(starter);
    	this.add(tekst);
				
	}

	@Override
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
		while(running) {
			
			
			if (playerNames) {
				checkMates();
			}
			
			try {
				Thread.sleep(400);
				int score = motherDialog.getTetrisPlayground().getMyScore();
				user1Score.setText(String.valueOf(score));
				
				score = motherDialog.getTetrisPlayground().getEnemyScore();
				user2Score.setText(String.valueOf(score));
				
				int rocketS = score / 50;
				if(rocket.getMax() < rocketS) {
					rocket.addAmount();
				}
				
				int helperS = score / 100;
				if(helper.getMax() < helperS) {
					helper.addAmount();
				}								
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		
	}
	
    /**
     * Metoda rysujaca (gďż˝ďż˝wnie) tďż˝o
     */
	@Override
	public void paintComponent(Graphics g) {
	    Graphics2D g2 = (Graphics2D) g;
	    
	    g2.drawImage(background, 0, 0, null);	    
	}
	
	public void setStop() {
		running = false;
	}

	public void setStart() {
		if(running == false) {
			running = true;
			Thread tr = new Thread(this);
			tr.start();		
		}
	}
	
	public Rocket getRocket() {
		return this.rocket;
	}
	
	public Helper getHelper() {
		return this.helper;
	}
	
	public void increaseRocket() {
		rocket.addAmount();
	}
	
	public void increaseHelper() {
		helper.addAmount();
	}	
	
	public void checkMates(){
		
		int id = room.getRoomId();
		System.err.println("Begin Check " + id);
		/*Vector<Room> vec = connector.getAllRooms();
		Room romek = null;
		int i = 1;
		for(Room rom : vec) {
			System.out.println(i);
			i++;
			if(rom.getRoomId() == id){
				romek = rom;
				break;
			}	
		}*/
		Room romek = connector.getRoom(room);
		
		if(romek != null) {
    	user1.setText(romek.getPlayers().get(0).getNick());
    	if(romek.getPlayers().size() > 1)
    		user2.setText(romek.getPlayers().get(1).getNick());
    	else
    		user2.setText("enemy");	
		} else System.err.println("DUPA");
		System.err.println("end check "+romek.getPlayers().size());
	}
}
